package com.gcj.study04jdk8.controller;

import com.gcj.study04jdk8.model.TestVO;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class TestVoUtil {


    // 将list转为map(并解决重复key问题)
    public static void Test(List<TestVO> list) {
        Map<Long, Integer> map1 = list.stream().collect(Collectors.toMap(TestVO::getId, TestVO::getAge, (v1, v2) -> v1));
        System.out.println("转为map的数据为" + map1);
    }

    //过滤指定查询条件(查询age>15并且age<18的数据)
    public static void Test1(List<TestVO> list) {
        List<TestVO> collect = list.stream().filter((e) ->
                e.getAge() > 15 && e.getAge() < 18
        ).collect(Collectors.toList());

        System.out.println("过滤后的数据为");
        TestVO.printStudents(collect);
    }
    public static void Test2(List<TestVO> list) {
        Map<Integer, List<TestVO>> map = list.stream().collect(Collectors.groupingBy(TestVO::getAge));
        System.out.println("根据指定列分组" + map);
    }

    public static void Test3(List<TestVO> list) {
        List<Integer> ageList = list.stream()
                .map(TestVO::getAge)
                .collect(Collectors.toList());

        System.out.println("通过map获取指定年龄列集合" + ageList);

    }
    //去重
    public static void Test4() {

        //测试数据，请不要纠结数据的严谨性
        List<TestVO> list = new ArrayList<>();
        list.add(new TestVO(1, "李小明", 18));
        list.add(new TestVO(2, "李小明", 19));
        list.add(new TestVO(3, "王大朋", 16));
        list.add(new TestVO(4, "陈小跑", 10));


        // 这里我们引入了两个静态方法，以及通过 TreeSet<> 来达到获取不同元素的效果
        // 1. import static java.util.stream.Collectors.collectingAndThen;
        // 2. import static java.util.stream.Collectors.toCollection;

        ArrayList<TestVO> collect = list.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(TestVO::getName))), ArrayList::new));
        TestVO.printStudents(collect);
    /*
            ----------------------------------------------------------
                    TestVO(id=1, name=李小明, age=18)
            TestVO(id=3, name=王大朋, age=16)
            TestVO(id=4, name=陈小跑, age=10)*/
    }
    public static void Test5() {
        //测试数据，请不要纠结数据的严谨性
        List<TestVO> list = new ArrayList<>();
        list.add(new TestVO(1, "李小明", 18));
        list.add(new TestVO(2, "李小明", 19));
        list.add(new TestVO(3, "王大朋", 16));
        list.add(new TestVO(4, "陈小跑", 10));

        double sum = list.stream().mapToDouble(TestVO::getAge).sum();//和
        OptionalDouble max = list.stream().mapToDouble(TestVO::getAge).max();//最大
        OptionalDouble min = list.stream().mapToDouble(TestVO::getAge).min();//最小
        OptionalDouble average = list.stream().mapToDouble(TestVO::getAge).average();//平均值

        System.out.println("和" + sum);
        System.out.println("最大" + max.getAsDouble());
        System.out.println("最小" + min.getAsDouble());
        System.out.println("平均值" + average.getAsDouble());

      /*  和63.0
        最大19.0
        最小10.0
        平均值15.75*/

    }
    //java8 stream 过滤、排序、求和、分组、去重等方法使用和说明
    public static void main(String[] args) {

        //测试数据，请不要纠结数据的严谨性
        List<TestVO> list = new ArrayList<>();
        list.add(new TestVO(1, "李小明", 18));
        list.add(new TestVO(2, "张小丽", 19));
        list.add(new TestVO(3, "王大朋", 16));
        list.add(new TestVO(4, "陈小跑", 10));


        list.stream().forEach(System.out::println);

        //按年龄排序(Integer类型)默认是升序，reversed()为降序
        List<TestVO> ageList1 = list.stream().sorted(Comparator.comparing(TestVO::getAge).reversed()).collect(Collectors.toList());
        TestVO.printStudents(ageList1);

    }
}

